#!/bin/bash
# Usage: script/cross-compile | script/github-release <tag>
#
# Takes in a list of asset filenames + labels via stdin and uploads them to the
# corresponding release on GitHub. The release is created as a draft first if
# missing and its body is the git changelog since the previous tagged release.
set -e

tag_name="${1?}"
[[ $tag_name == *-* ]] && pre=1 || pre=

assets=()
while read -r filename label; do
  assets+=( -a "${filename}#${label}" )
done

if hub release --include-drafts | grep -q "^${tag_name}\$"; then
  hub release edit "$tag_name" -m "" "${assets[@]}"
else
  git tag --list "$tag_name" --format='%(contents:subject)%0a%0a%(contents:body)' | \
    hub release create ${pre:+--prerelease} -F- "$tag_name" "${assets[@]}"
fi
